const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //拍摄的图片
    img: '',
    //摄像头位置
    devicePosition: 'back'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 返回用户信息页面
   */
  toUserDescPage() {
    wx.navigateTo({
      url: '/pages/user/desc/desc',
    })
  },
  /**
   * 切换摄像头
   */
  onChangeCamera() {
    if (this.data.devicePosition == "back") {
      this.setData({
        devicePosition: "front"
      })
    } else {
      this.setData({
        devicePosition: "back"
      })
    }
  },
  /**
   * 拍摄
   */
  takePhotoHandle() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          img: res.tempImagePath
        })
        //修改用户头像
        wx.getStorage({
          key: "userId",
          success: res => {
            //console.log(res.data)
            postRequest(baseUrl + "/front/user/updateUserImg", {
              userId: res.data,
              userImg: res.tempImagePath
            }).then(res => {
              if (res.code == 200) {
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            })
          }
        })
        //返回用户界面
        wx.navigateTo({
          url: '/pages/user/desc/desc?userImg=' + res.tempImagePath,
        })
      }
    })
  },
  
})